﻿using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace ActivateYourGlutes.Helpers
{
    public static class LinkHelpers
    {
        private const int HTTPS = 443;
        private const int HTTP = 80;

        public static string InsecureActionLink(RequestContext requestContext, HttpContextBase httpContext, string linkText, string action, string controller)
        {
            var urlHelper = new UrlHelper(requestContext);
            var url = urlHelper.Action(action, controller);
            var builder = new UriBuilder
            {
                Scheme = "http",
                Host = httpContext.Request.Url.Host,
                // use the RawUrl since it works with URL Rewriting 
                Path = url
            };
            var port = httpContext.Request.Url.Port;
            if (port != HTTPS && port != HTTP)
                builder.Port = port;
            if (String.IsNullOrEmpty(linkText))
                return builder.ToString();
            return String.Format("<a href=\"{0}\">{1}</a>", builder, linkText);
        }
    }
}
